var AWS = require('aws-sdk')
import generateID from '@/utils/generateID'

export function AwsUpdate(file, options) {
  // Set the region
  AWS.config.update({
    accessKeyId: options.accessKeyId,
    secretAccessKey: options.secretKey,
    sessionToken: options.sessionToken,
    // apiVersion: '2006-03-01',
    region: options.region
  })

  // create aws service
  var s3 = new AWS.S3()
  const date = new Date()
  let key = `${options.dir}/${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}/${generateID()}${file.name.substring(file.name.lastIndexOf('.'))}`
  console.log('options::', options)
  var uploadParams = {
    Bucket: options.bucketName, // Bucket Name
    Key: key, //file.name, // pic Name
    Body: file, // file
    ACL: 'public-read', // type
    'Access-Control-Allow-Origin': '*'
  }
  console.log(uploadParams)
  var imgFile = new Promise(function (resolve) {
	  uni.showLoading({
	  	title: 'loading'
	  })
    s3.upload(uploadParams, function (err, data) {
		uni.hideLoading()
      if (err) {
        console.log('Error', err)
      }
      if (data) {
        resolve(data)
      }
    })
  })
  return imgFile
}
